w<template>
  <el-container class="el-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="title">
        <img src="../assets/logo.png" alt="" />
        <span>就爱科技软件二班一部招标项目</span>
      </div>
      <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link">
          {{ adminName }}管理员<i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="ChangePassword()"
            >修改密码</el-dropdown-item
          >
          <el-dropdown-item command="logout">退出</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
        >
          <!-- 用户管理一级菜单 -->
          <el-submenu index="1">
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i class="el-icon-user-solid"></i>
              <!-- 文本 -->
              <span>用户管理</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item index="users">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-s-custom"></i>
                <!-- 文本 -->
                <span>用户列表</span>
              </template>
            </el-menu-item>
            <!-- 管理员列表 -->
            <el-menu-item index="admins">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-s-custom"></i>
                <!-- 文本 -->
                <span>管理员列表</span>
              </template>
            </el-menu-item>
          </el-submenu>
          <!-- 文章管理一级菜单 -->
          <el-submenu index="2">
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i class="el-icon-edit"></i>
              <!-- 文本 -->
              <span>文章管理</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item index="posts">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-edit-outline"></i>
                <!-- 文本 -->
                <span>文章列表</span>
              </template>
            </el-menu-item>
            <!-- 评论列表 -->
            <el-menu-item index="comment">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-news"></i>
                <!-- 文本 -->
                <span>评论列表</span>
              </template>
            </el-menu-item>
            <!-- 文章分类 -->
            <el-menu-item index="category">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-s-grid"></i>
                <!-- 文本 -->
                <span>文章分类</span>
              </template>
            </el-menu-item>
          </el-submenu>
          <!-- 网站信息管理一级菜单 -->
          <el-submenu index="3">
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i class="el-icon-coin"></i>
              <!-- 文本 -->
              <span>系统管理</span>
            </template>
            <!-- 二级菜单 -->
            <!-- 二维码管理 -->
            <el-menu-item index="qrcode">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-picture-outline"></i>
                <!-- 文本 -->
                <span>二维码管理</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
    <!-- 修改密码对话框区域 -->
    <el-dialog
      title="修改密码"
      :visible.sync="ChangeVisible"
      width="50%"
      @close="editDialogclosed"
    >
      <!-- 内容主体区域 -->
      <el-form
        label-width="70px"
        :model="ChangeForm"
        :rules="ChangeFormRules"
        ref="editFormRef"
      >
        <!-- 密码 -->
        <el-form-item label="密码" prop="adminpwd">
          <el-input v-model="ChangeForm.adminpwd"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <!-- 底部按钮区域 -->
        <el-button @click="ChangeVisible = false">取 消</el-button>
        <el-button type="primary" @click="PutPassWord()">确 定</el-button>
      </span>
    </el-dialog>
  </el-container>
</template>

<script>
import '../assets/css/global.css'
export default {
  mounted () {
    this.adminId = parseInt(window.sessionStorage.getItem('id'))
    this.adminName = window.sessionStorage.getItem('adminName')
  },
  data () {
    return {
      // 是否折叠
      isCollapse: false,
      adminName: '',
      adminId: 0,
      // 控制修改密码对话框的隐藏和显示，默认为false
      ChangeVisible: false,
      // 修改密码表单数据绑定
      ChangeForm: {
        adminpwd: ''
      },
      ChangeFormRules: {
        adminname: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        adminpwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {

  },
  methods: {
    // 点击下拉菜单出发的事件
    handleCommand (command) {
      if (command === 'logout') {
        window.sessionStorage.clear()
        this.$router.push('/login')
      }
    },
    // 点击折叠，切换菜单的折叠和展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 修改密码
    ChangePassword () {
      this.ChangeVisible = true
    },
    // 修改密码
    async PutPassWord () {
      this.ChangeVisible = false
      console.log(this.ChangeForm.adminpwd)
      await this.$http.put(`admin/${this.adminId}`, this.ChangeForm)
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 监听修改密码对话框关闭事件
    editDialogclosed () {
      this.$refs.editFormRef.resetFields()
    }
  }
}
</script>

<style scoped>
.el-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  font-size: 20px;
}
.title {
  display: flex;
  align-items: center;
  color: #fff;
}
span {
  margin-left: 15px;
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.el-menu {
  border-right: none;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
